package com.hanscan.persistence.controllers;


import com.hanscan.persistence.EntityManagerSingleton;
import com.hanscan.persistence.controllers.exceptions.NonexistentEntityException;
import com.hanscan.persistence.entities.EventType;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

public class EventTypeJpaController implements Serializable
{

    public EventTypeJpaController()
    {
    }

    public EntityManager getEntityManager()
    {
        return EntityManagerSingleton.getInstance();
    }

    public void create(EventType eventType) throws Exception
    {
        //TODO: comprobaciones
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(eventType);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void edit(EventType eventType) throws NonexistentEntityException, Exception
    {
        //TODO: comprobaciones
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            eventType = em.merge(eventType);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void destroy(EventType eventType) throws Exception
    {
        //TODO: comprobaciones
        //TODO: si se borra usuario, se borran tb sus localizaciones, a no ser q el borrado sea logico
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            em.remove(eventType);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public List<EventType> findEventTypeEntities()
    {
        return findEventTypeEntities(true, -1, -1);
    }

    public List<EventType> findEventTypeEntities(int maxResults, int firstResult)
    {
        return findEventTypeEntities(false, maxResults, firstResult);
    }

    private List<EventType> findEventTypeEntities(boolean all, int maxResults,
            int firstResult)
    {
        EntityManager em = getEntityManager();
        try
        {
            Query q = em.createQuery("select object(o) from EventType as o");
            if (!all)
            {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        }
        finally
        {
            em.close();
        }
    }

    public EventType findEventType(Long id)
    {
        EntityManager em = getEntityManager();
        try
        {
            return em.find(EventType.class, id);
        }
        finally
        {
            em.close();
        }
    }

    public int getEventTypeCount()
    {
        EntityManager em = getEntityManager();
        try
        {
            Query q = em.createQuery("select count(o) from EventType as o");
            return ((Long) q.getSingleResult()).intValue();
        }
        finally
        {
            em.close();
        }
    }






    /**
     *
     * @return
     * @throws com.hanscan.controllers.exceptions.NonexistentEntityException
     */
    public EventType findIdentifyOKEventType() throws
            NonexistentEntityException
    {
        EntityManager em = getEntityManager();
        EventType eventType = null;
        try
        {
            Query query = em.createNamedQuery("findIdentifySuccessEventType");
            eventType = (EventType) query.getSingleResult();
            return eventType;
        }
        catch (NoResultException ex)
        {
            throw new NonexistentEntityException(ex.getMessage());
        }
    }



    /**
     *
     * @return
     * @throws com.hanscan.controllers.exceptions.NonexistentEntityException
     */
    public EventType findIdentifyFailEventType() throws
            NonexistentEntityException
    {
        EntityManager em = getEntityManager();
        EventType eventType = null;
        try
        {
            Query query = em.createNamedQuery("findIdentifySuccessEventType");
            eventType = (EventType) query.getSingleResult();
            return eventType;
        }
        catch (NoResultException ex)
        {
            throw new NonexistentEntityException(ex.getMessage());
        }
    }

    
    
}
